%%%-------------------------------------------------------------------
%%% @author Administrator
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 14. 10月 2019 17:38
%%%-------------------------------------------------------------------
-module(myshare).
-author("Administrator").

-include("include/logger.hrl").
-include("include/myshare.hrl").

%% API
-export([get_now_str/0,init_env/0,get_env/1,set_env/2]).

%% 返回当前时间的字符串2012-8-6 11:11:15
%% 添加Padding保证格式一致
get_now_str() ->
  {{Year, Month, Day}, _} = erlang:localtime(),
  MonthPadding = io_lib:format("~2..0B",[Month]),%%整数转字符串
  DayPadding = io_lib:format("~2..0B",[Day]),
  lists:concat([Year, '-', MonthPadding, '-', DayPadding]).

%%存储配置文件到ets
init_env()->
  ets:new(?ETS_CACHE_SERVER_ENV, [set, public, named_table, {read_concurrency, true}]),
  ConfigList = application:get_all_env(),
  Fun = fun({Key, Val}) ->
    ets:insert(?ETS_CACHE_SERVER_ENV, {Key, Val})
        end,
  lists:foreach(Fun, ConfigList).


set_env(Key,Value)->
  ets:insert(?ETS_CACHE_SERVER_ENV, {Key,Value}).


%% get env value by key
get_env(Key) ->
  try
    ets:lookup_element(?ETS_CACHE_SERVER_ENV, Key, 2)
  catch
    error:badarg -> undefined
  end.

